<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of BeerList
 *
 * @author moon
 */
class BeerList {

    //put your code here
    public $dept = '';   //"category": "POP",
    public $stock = '';  //"product_id": 1,
    public $name = '';   //"artist": "Rihana",
    public $currprice = 0.0;  //"currprice": 15.00
    public $lastprice = 0.0;  //"lastprice": 15.00
    public $image = '';  //"img": "cd4.jpg",
    public $description = '';    //"album": "Rihana",
    public $stockavailable = 0;
    public $change = 0.0;
    public $changeperc = 0.0;
    public $openprice = 0.0;
    public $crashed = false;
	public $apply_marketcrash_sound = false;
	public $play_pricechanged_sound = false;
}

include 'config.php';
$json_code = '';
$apply_marketcrash_sound = false;
$play_pricechanged_sound = false;

try {
    /*        $db = new PDO(
      "mysql:host=$db_host;dbname=$db_name;", $db_user, $db_pass
      );
     */
    $db = new PDO("odbc:$odbc_name", '', '');

    $db->query("SET NAMES 'utf8'");
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch (PDOException $e) {
    error_log($e->getMessage());
    die("A database error was encountered, maybe ODBC name $odbc_name was not set in the OS yet.");
}

$query = 'select * from bexconfig';
$stmt = $db->prepare("$query");
$stmt->execute();
$row = $stmt->fetch(PDO::FETCH_ASSOC);
$apply_marketcrash_sound = $row['soundeffec'];
$play_pricechanged_sound = $row['prcchgdsnd'];
unset($stmt);


try {
    //stock.info2 char(40), stock.info3 char(18), stock.descrip1 char(40)

    $query = "select p.*, s.remark1 from productsettings as p inner join stock as s on p.stock = s.stock where selected = .t. order by remark1";
    $stmt = $db->prepare("$query");
    $stmt->execute();

    $json = array();

    while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
        $beer = new BeerList();
        $beer->dept = $dept;
        $beer->stock = trim($row['stock']);
        $beer->name = trim($row['name']);
        $beer->currprice = $row['currprice'];
        $beer->lastprice = $row['lastprice'];
        $beer->description = trim($row['remark1']);
        $beer->image = "$beer->name.png";
        if (!file_exists("img/$beer->image")) {
            $beer->image = "no_image.png";
        }
        $beer->stockavailable = $row['qty'] - $row['totalsold'];
        if ($beer->stockavailable < 0) {
            $beer->stockavailable = 0;
        }
        $beer->change = $row['change'];
        $beer->changeperc = $row['changeperc'];
        $beer->openprice = $row['openprice'];
        $beer->crashed = $row['crashed'];
		$beer->apply_marketcrash_sound = $apply_marketcrash_sound;
		$beer->play_pricechanged_sound = $play_pricechanged_sound;
        $json[] = $beer;
    }
    $json_code = json_encode($json, JSON_NUMERIC_CHECK | JSON_PRETTY_PRINT);
	unset($stmt);
} catch (PDOException $e) {
    print $e->getMessage();
}
unset($db);

if ($generate_json_file) {
    $file = fopen('data/' . $dept . '_data.json', 'w');
    fwrite($file, $json_code);
    fclose($file);
}
echo $json_code;